package com.liyujie.serialport.utils;

import android.util.Log;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.liyujie.serialport.listener.CreditCardCallbackInterface;
import com.liyujie.serialport.mobile.SKData;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import in.xiandan.countdowntimer.CountDownTimerSupport;
import in.xiandan.countdowntimer.OnCountDownTimerListener;
import me.f1reking.serialportlib.SerialPortHelper;
import me.f1reking.serialportlib.entity.DATAB;
import me.f1reking.serialportlib.entity.FLOWCON;
import me.f1reking.serialportlib.entity.PARITY;
import me.f1reking.serialportlib.entity.STOPB;
import me.f1reking.serialportlib.listener.IOpenSerialPortListener;
import me.f1reking.serialportlib.listener.ISerialPortDataListener;
import me.f1reking.serialportlib.listener.Status;

/**
 * 刷卡串口
 */
public class CreditCardSerialPortUtils {

    public static SerialPortHelper serialPortHelper;
    private static StringBuffer buffer = new StringBuffer();
    //倒计时
    private static CountDownTimerSupport countDownTimerSupport = new CountDownTimerSupport(500, 100);

    /**
     * 初始刷卡串口
     */
    public static void initializationCreditCardSerialPort() {
        serialPortHelper = new SerialPortHelper();
        serialPortHelper.setIOpenSerialPortListener(new IOpenSerialPortListener() {
            @Override
            public void onSuccess(File device) {
                Log.d("OpenSerialportUtils","成功"+device.getPath());
            }

            @Override
            public void onFail(File device, Status status) {
                switch (status) {
                    case NO_READ_WRITE_PERMISSION:
                        Log.d("OpenSerialportUtils",device.getPath() + " :没有读写权限");
                        break;
                    case OPEN_FAIL:
                    default:
                        Log.d("OpenSerialportUtils",device.getPath() + " :串口打开失败");
                        break;
                }
            }
        });

        //倒计时
        countDownTimerSupport.setOnCountDownTimerListener(new OnCountDownTimerListener() {
            @Override
            public void onTick(long millisUntilFinished) {

            }
            @Override
            public void onFinish() {
                EventBus.getDefault().post(new SKData(buffer.toString()));
                buffer = new StringBuffer();
            }

            @Override
            public void onCancel() {

            }
        });
    }


    /**
     * 打开刷卡串口
     * @param serialPortAddress
     * @return
     */
    public static boolean openCreditCardSerialPort(String serialPortAddress){
        boolean whetherOpen = false;
        //打开串口
        if(null != serialPortHelper){
            if(serialPortHelper.isOpen()){
                Log.d("OpenSerialportUtils","刷卡串口开启成功");
                whetherOpen = true;
            }else {
                serialPortHelper.setPort(serialPortAddress);
                serialPortHelper.setBaudRate(9600);
                serialPortHelper.setStopBits(STOPB.getStopBit(STOPB.B1));
                serialPortHelper.setDataBits(DATAB.getDataBit(DATAB.CS8));
                serialPortHelper.setParity(PARITY.getParity(PARITY.NONE));
                serialPortHelper.setFlowCon(FLOWCON.getFlowCon(FLOWCON.NONE));
                //定义串口数据接收回调
                serialPortHelper.setISerialPortDataListener(new ISerialPortDataListener() {
                    @Override
                    public void onDataReceived(byte[] bytes) {
                        String swipingCard = ConvertUtils.bytes2HexString(bytes);
                        if(countDownTimerSupport.isStart()){
                            if(buffer.length() > 0){
                                buffer.append(swipingCard);
                            }
                        }else {
                            buffer.append(swipingCard);
                            countDownTimerSupport.reset();
                            countDownTimerSupport.start();
                        }
                    }

                    @Override
                    public void onDataSend(byte[] bytes) {

                    }
                });
                if(serialPortHelper.open()){
                    Log.d("OpenSerialportUtils","刷卡串口开启成功");
                    whetherOpen = true;
                }else {
                    whetherOpen = false;
                    Log.d("OpenSerialportUtils","刷卡串口开启失败");
                }
            }
        }
        return whetherOpen;
    }
}
